﻿<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">   
    
    void InsertMsg(string msg)
    {
        ListBoxEvents.Items.Insert(0, msg);
        ListBoxEvents.SelectedIndex = 0;
    }
    void SubmitButton_Click(object sender, EventArgs e)
    {
        InsertMsg("你上传了" + uploadcount + "/" + Uploader1.Items.Count + " 文件.");
    }

    int uploadcount = 0;
    public StringBuilder sb = new StringBuilder();
    void Uploader_FileUploaded(object sender, UploaderEventArgs args)
    {
        uploadcount++;
        Uploader uploader = (Uploader)sender;
        InsertMsg("文件上传 " + args.FileName + ", " + args.FileSize + " 字节.");
        string Path = Server.MapPath("~/ImageProductsTemp") + "\\" + args.FileName;
        args.CopyTo(Path);
        Golf.WebUI.AdminShow.CommoditiesMmanagement.Golf_ProductAdd add = new Golf.WebUI.AdminShow.CommoditiesMmanagement.Golf_ProductAdd();
        string resault = add.SaveImg(args.FileName, Path);//赋值给隐藏域 
        sb.Append(resault + "|");
        if (sb.Length != 0)
        {
            FileName.Value = sb.ToString().Substring(0, sb.Length - 1);
        }

        //Copys the uploaded file to a new location.
        //args.CopyTo(path);
        //You can also open the uploaded file's data stream.
        //System.IO.Stream data = args.OpenStream();
    }

    //protected override void OnPreRender(EventArgs e)
    //{
    //    SubmitButton.Attributes["itemcount"] = Uploader1.Items.Count.ToString();

    //    base.OnPreRender(e);
    //}
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (GetVisibleItemCount() >= 1)
        {
            Uploader1.InsertButton.Enabled = false;
            SubmitButton.Enabled = false;
        }
        else
        {
            Uploader1.InsertButton.Enabled = true; SubmitButton.Enabled = true;
        }
    }
    int GetVisibleItemCount()
    {
        int count = 0;
        foreach (AttachmentItem item in Uploader1.Items)
        {
            if (item.Visible)
                count++;
        }
        return count;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
</script>

<script src="../../Script/jquery/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>

<script type="text/javascript">
    $(function () {
        //window.parent.frames.header.location.reload();
        //alert($(window.parent.frames.header).find("#").);

        function show() {
            var state = $("#state").val();
            if (state == "0") {
                $('#btnSave', window.parent.document).attr("disabled", "disabled");
            } else {
                $('#btnSave', window.parent.document).attr("disabled", "");
            }
        }
        setInterval(show, 1); // 注意函数名没有引号和括弧！
    });
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>文件上传</title>
</head>
<body>
    <form id="form1" runat="server">
    <div class="content">
        <CuteWebUI:UploadAttachments runat="server" ManualStartUpload="true" ID="Uploader1"
            InsertText="浏览文件(最大 100M)" OnFileUploaded="Uploader_FileUploaded" FileTooLargeMsg="{0}无法上载！文件大小 ({1}) 太大。最大文件尺寸允许设置: {2}."
            MaxFilesLimitMsg="文件的最大数量允许上传设置为 {0}." UploadingMsg="正在上传" RemoveButtonBehavior="Delete"
            RemoveButtonText="删除" TableHeaderTemplate="<td></td><td>文件</td>" ShowCheckBoxes="False"
            ShowFileIcons="False" CancelAllMsg="取消所有上传" CancelText="取消" CancelUploadMsg="取消上传"
            MultipleFilesUpload="true" ShowRemoveButtons="False" UploadProcessingMsg="13134"
            ProgressTextTemplate="正在上传：%F%&nbsp;&nbsp;%SEND%/%SIZE%&nbsp;&nbsp;%KBPS%&nbsp;&nbsp;已完成：%P%&nbsp;&nbsp;估计剩余时间：%T%秒

">
            <ValidateOption MaxSizeKB="102400" AllowedFileExtensions="jpeg,jpg,png,BMP" />
        </CuteWebUI:UploadAttachments>
        <br />
        <asp:Button runat="server" ID="SubmitButton" OnClientClick="return submitbutton_click()"
            Text="上传" OnClick="SubmitButton_Click" />
        <input type="hidden" id="FileName" value="0" runat="server" />
        <input type="hidden" id="state" value="-1" runat="server" />
        <br />
        <div>
            <asp:ListBox runat="server" ID="ListBoxEvents" Width="400"></asp:ListBox>
        </div>

        <script type="text/javascript">

            function submitbutton_click() {
                var submitbutton = document.getElementById('<%=SubmitButton.ClientID %>');
                var uploadobj = document.getElementById('<%=Uploader1.ClientID %>');
                if (!window.filesuploaded) {
                    if (uploadobj.getqueuecount() > 0) {
                        document.getElementById("state").value = "0"; //上传的状态
                        uploadobj.startupload();
                    }
                    else {
                        var uploadedcount = parseInt(submitbutton.getAttribute("itemcount")) || 0;
                        if (uploadedcount > 0) {
                            return true;
                        }
                        alert("请选择文件");
                    }
                    return false;
                }
                document.getElementById("state").value = "-1"; //上传的状态
                window.filesuploaded = false;
                return true;
            }
            function CuteWebUI_AjaxUploader_OnPostback() {
                window.filesuploaded = true;
                var submitbutton = document.getElementById('<%=SubmitButton.ClientID %>');
                submitbutton.click();
                document.getElementById("state").value = "-1"; //上传的状态
                return false;
            }
        </script>

    </div>
    </form>
</body>
</html>
